package com.ljfiction.book.activitys;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.ljfiction.book.MyView.ContainsEmojiEditText;
import com.ljfiction.book.R;
import com.ljfiction.book.http.HttpManager;
import com.ljfiction.book.tools.SettingManager;
import com.ljfiction.book.tools.StringTool;
import com.lzy.okhttputils.OkHttpUtils;
import com.lzy.okhttputils.callback.StringCallback;

import org.json.JSONObject;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.Call;
import okhttp3.Response;

public class CreadVolumNameActivity extends BaseActivity {

    @BindView(R.id.cread_volum_name)
    ImageView CreadVolumName;
    @BindView(R.id.creadvolumnameok)
    TextView creadvolumnameok;
    @BindView(R.id.name_cread)
    ContainsEmojiEditText nameCread;

    private String bookId,bookName;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_cread_volum_name);
        ButterKnife.bind(this);
        bookId = getIntent().getStringExtra("bookId");
        bookName = getIntent().getStringExtra("bookName");
    }

    @OnClick({R.id.cread_volum_name, R.id.creadvolumnameok})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.cread_volum_name:
                goBack();
                break;
            case R.id.creadvolumnameok:
                if (StringTool.isEmpty(nameCread.getText().toString())){
                    showToast("卷名不能为空");
                    return;
                }else if(!StringTool.verifyBookName(nameCread.getText().toString())){
                    showToast("卷名只支持汉字、数字、字母和冒号");
                    return;
                }else {
                    toIsVolunName();
                }

                    break;
        }
    }

    private void toIsVolunName() {
        OkHttpUtils.post(HttpManager.IP+"/books-getCheckVolume")

                .params("volumeName",nameCread.getText().toString().trim())
                .params("bookid",bookId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONObject js=new JSONObject(s);
                            if (js.getInt("returnCode")==200){
                                toCreadVolum();
                            }else {
                                showToast(js.getString("msg"));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }

    private void toCreadVolum() {
        OkHttpUtils.post(HttpManager.IP+"/books-addvolume")

                .params("volumeName",nameCread.getText().toString().trim())
                .params("bookName",bookName)
                .params("bookid",bookId)
                .execute(new StringCallback() {
                    @Override
                    public void onSuccess(String s, Call call, Response response) {
                        try {
                            JSONObject js=new JSONObject(s);
                            if (js.getInt("returnCode")==200){
                                showToast(js.getString("msg"));
                                setResult(2);
                                goBack();
                            }else {
                                showToast(js.getString("msg"));
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                });
    }
}
